//
//  PayAndQRCController.m
//  MBObjectServer
//
//  Created by WorkSpace on 4/4/14.
//  Copyright (c) 2014 MBpayer. All rights reserved.
//

#import "PayAndQRCController.h"

#pragma mark 结算
@implementation PayAndQRCController

-(void)DymicCalcActSaleObjs:(NSMutableArray *)billSaleDetails
          ShopCarArrayCount:(NSNumber *)shopCarArrayCount
                     SumPay:(int)sumPay
              DiscountModel:(DiscountMesModel *)discountModel
                  LocalTime:(NSString *)localTime
            CouponCardModel:(EPOSCcCouponCardInfo *)couponCardModel
                    Success:(ReceiveSuccessPay)receiveSuccessPay
                    Failure:(ReceiveFailure)processFailure
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////处理BillSaleHead
    //传: self.shopCardArray(count)购物车数量   ,   sumpay总价    ,discountmodel折扣的model    ,LocalTime时间
    billheadinfo = [[EPOSBillHeadInfo alloc] init];
    billheadinfo.OrgCode = [userDefaults objectForKey:kOrgCode];
    billheadinfo.OwnerOrgCode = [userDefaults objectForKey:kOwnerOrgCode];
    billheadinfo.IsUpload = [NSNumber numberWithInt:0];
    billheadinfo.CashierUserID = [userDefaults objectForKey:kUserID];
    
    //    折上折
    NSString * discountRate2Str = [userDefaults objectForKey:kdiscountRate2];
    if (discountRate2Str.length>0)
    {
        billheadinfo.DiscountRate = [NSNumber numberWithInt:[discountRate2Str intValue]];
    }
    else
    {
        billheadinfo.DiscountRate = [NSNumber numberWithInt:100];
    }
//    billheadinfo.DiscountReason = commvct.discountReason.text;//整单折扣折扣原因
    billheadinfo.CardOrderId = [[NSMutableArray alloc] initWithObjects:@"支付宝", nil];
    billheadinfo.SaleType = @"RT";
    billheadinfo.TotalQty = shopCarArrayCount;//购物车数量
    billheadinfo.TotalMoney = [NSNumber numberWithFloat:sumPay];//总价
    if (discountModel.DiscountPrice > 0)//折扣的model
    {
        billheadinfo.DiscountMoney = discountModel.DiscountPrice;
    }
    else
    {
        billheadinfo.DiscountMoney = [NSNumber numberWithFloat:0.00];
    }
    billheadinfo.IsTest = [NSNumber numberWithBool:false];
    //    billheadinfo.CashierUserID = [userDefaults objectForKey:kUserID];
    billheadinfo.CashierID = [userDefaults objectForKey:kCashierID];
    billheadinfo.CashierTeamID = [userDefaults objectForKey:kCashierTeamID];
    billheadinfo.VerID = @"IPad1.0";
    billheadinfo.VIPCode = [userDefaults objectForKey:kVIPCode];
    billheadinfo.LocalTime = localTime;
    NSMutableDictionary * billheaddic = [EPOSBillHeadInfo getDictionaryFromEPOSBillHeadInfo:billheadinfo];
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////处理CouponCards
    //传couponcardmodel
    NSMutableDictionary *ccCouponCardInfo = [[NSMutableDictionary alloc]init];
    NSMutableArray * couponarray = [[NSMutableArray alloc] init];
    if (!(couponCardModel == nil))
    {
        ccCouponCardInfo = [EPOSCcCouponCardInfo getDictionaryFromEPOSCcCouponCardInfo:couponCardModel];
        [couponarray addObject:ccCouponCardInfo] ;
    }
    
    
    
    NSMutableDictionary * postUpdata = [[NSMutableDictionary alloc] initWithObjectsAndKeys:billheaddic,@"BillSaleHead",billSaleDetails,@"BillSaleDetails",couponarray,@"CouponCards", nil];
    MBBaseServer *baseServer = [[MBBaseServer alloc]init];
    [baseServer setRequestStyle:nil andSetPathString:@"billsale_handler.ashx" andSetHeadString:nil andActionName:@"DymicCalcActSaleObjs" andSetMainDictionary:postUpdata block:^(id json)
     {
         NSNumber * VoucherValue = [json objectForKey:@"VoucherValue"];//代金券值
         NSDictionary * headinfoDic = [json objectForKey:@"HeadEntity"];//应付价格
         EPOSBillHeadInfo * headinfo = [EPOSBillHeadInfo getEPOSBillHeadInfoFromDictionary: headinfoDic ];
         float paymoney = [headinfo.TotalMoney floatValue] - [VoucherValue floatValue];//应付价=总价-打折券金额
         if (paymoney<0)
         {
             paymoney = 0.00;
         }
         NSNumber *real = [NSNumber numberWithFloat:paymoney];
         NSNumber *all = [NSNumber numberWithFloat:[headinfo.TotalMoney floatValue]];
         
         NSNumber *tic = [NSNumber numberWithFloat:[VoucherValue floatValue]];
         NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
         [userDefaults setObject:all forKey:kallPayStr];
         [userDefaults setObject:real forKey:krealPayStr];
         [userDefaults setObject:tic forKey:kticketStr];
         
         //返回的json传到Pay页面去做处理,json为返回原数组，用来Save接口提取HeadEntity，DetailEntitys
         //save接口需要参数：json字典中的HeadEntity，DetailEntitys和couponcardmodel。
         receiveSuccessPay(json);
     }
     failblock:^(NSError *error)
     {
         processFailure(error);
     }];
    
}

#pragma mark 二维码
-(void)PayRequest:(NSString *)type
           Amount:(NSNumber *)amount
          Success:(ReceiveSuccessQRC)receiveSuccessQRC
          Failure:(ReceiveFailure)processFailure
{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *postUpdata = [NSMutableDictionary dictionaryWithObjectsAndKeys:[userDefault objectForKey:kOrgCode],@"OrgCode",type,@"Type",amount,@"Amount",nil];
    
    MBBaseServer *baseServer = [[MBBaseServer alloc]init];
    [baseServer setRequestStyle:nil andSetPathString:@"pay_request.ashx" andSetHeadString:nil andActionName:@"PayRequest" andSetMainDictionary:postUpdata block:^(id json)
     {
         bankEntityResult = [EPOSBankEntityResult getEPOSBaseResultFromDictionary:json];
         bankEntity = [EPOSBankEntity getEPOSBankEntityFromDictionary:bankEntityResult.BankEntity];
         receiveSuccessQRC(bankEntityResult,bankEntity);
     }
    failblock:^(NSError *error)
     {
         processFailure(error);
     }];
}
#pragma mark Save接口

-(void)Save:    (NSMutableDictionary *)receiveJson
CouponCardModel:(EPOSCcCouponCardInfo *)couponCardModel
bankEntitySave:(EPOSBankEntity*)bankEntitySave
    Success:(ReceiveSuccessSave)receiveSuccessSave
    Failure:(ReceiveFailure)processFailure
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //拼接
    payitem_Cash = [[PayItemCash alloc] init];
    payitem_Cash.OrgCode = [userDefaults objectForKey:kOrgCode];
    payitem_Cash.OwnerOrgCode = [userDefaults objectForKey:kOwnerOrgCode];
    payitem_Cash.PayCode = @"Cash";
    payitem_Cash.Change = [NSNumber numberWithFloat:0.0];
    payitem_Cash.PayValue = [NSNumber numberWithFloat:0.0];
    payitem_Cash.ActPayValue = [NSNumber numberWithFloat:0.0];
    payitem_Cash.IsOOS = [NSNumber numberWithBool:false];
    
    NSMutableDictionary * dic_Cash = [PayItemCash getDictionaryFromPayItemCash:payitem_Cash];
    
    payitem_Card =[[PayItemCash alloc] init];
    payitem_Card.OrgCode = [userDefaults objectForKey:kOrgCode];
    payitem_Card.OwnerOrgCode = [userDefaults objectForKey:kOwnerOrgCode];
    payitem_Card.PayCode = @"Card";
    payitem_Card.Change = [NSNumber numberWithFloat:0.0];
    payitem_Card.PayValue = [NSNumber numberWithFloat:0.0];//这个值使用应付金额
    payitem_Card.ActPayValue = [NSNumber numberWithFloat:0.0];//这个值也是使用应付金额
    payitem_Card.IsOOS = [NSNumber numberWithBool:false];
    NSMutableDictionary * dic_Card = [PayItemCash getDictionaryFromPayItemCash:payitem_Card];
    
    
    payitem_Voucher =[[PayItemCash alloc] init];
    payitem_Voucher.OrgCode = [userDefaults objectForKey:kOrgCode];
    payitem_Voucher.OwnerOrgCode = [userDefaults objectForKey:kOwnerOrgCode];
    payitem_Voucher.PayCode = @"Voucher";
    payitem_Voucher.Change = [NSNumber numberWithFloat:0.0];
    
    if ([@"会员券" isEqualToString:couponCardModel.DISP_NAME]) {
        payitem_Voucher.PayValue = couponCardModel.TRAN_VAL;//代金券值
    }else{
        payitem_Voucher.PayValue = [NSNumber numberWithFloat:0.0];//代金券值
    }
    payitem_Voucher.ActPayValue = [NSNumber numberWithFloat:0.0];//代金券值
    payitem_Voucher.IsOOS = [NSNumber numberWithBool:false];
    
    NSMutableDictionary * dic_Voucher = [PayItemCash getDictionaryFromPayItemCash:payitem_Voucher];
    
    payitem_CashVouc =[[PayItemCash alloc] init];
    payitem_CashVouc.OrgCode = [userDefaults objectForKey:kOrgCode];
    payitem_CashVouc.OwnerOrgCode = [userDefaults objectForKey:kOwnerOrgCode];
    payitem_CashVouc.PayCode = @"CashVouc";
    payitem_CashVouc.Change = [NSNumber numberWithFloat:0.0];
    payitem_CashVouc.PayValue = [NSNumber numberWithFloat:0.0];
    payitem_CashVouc.ActPayValue = [NSNumber numberWithFloat:0.0];
    payitem_CashVouc.IsOOS = [NSNumber numberWithBool:false];
    NSMutableDictionary * dic_CashVouc = [PayItemCash getDictionaryFromPayItemCash:payitem_CashVouc];
    
    
    
    //修改cash和v
    if ([[userDefaults objectForKey:krealPayStr] floatValue]==0)
    {
        payitem_Voucher.PayValue = [userDefaults objectForKey:kticketStr];
        payitem_Voucher.ActPayValue = [userDefaults objectForKey:kallPayStr];
    }
    else
    {
        payitem_Voucher.PayValue = [userDefaults objectForKey:kticketStr];
        payitem_Voucher.ActPayValue = [userDefaults objectForKey:kticketStr];
    }
    payitem_Card.PayValue = [userDefaults objectForKey:krealPayStr];
    payitem_Card.ActPayValue = [userDefaults objectForKey:krealPayStr];
    
    
    NSArray * PayEntitys = [[NSArray alloc] initWithObjects:dic_Card,dic_Cash,dic_CashVouc,dic_Voucher, nil];
    //取得另外三个对象
    NSMutableDictionary * posHeadEntity = [receiveJson objectForKey:@"HeadEntity"];
    NSArray * posDetailEntitys = [receiveJson objectForKey:@"DetailEntitys"];
    NSMutableDictionary * ccouponcarddic = [EPOSCcCouponCardInfo getDictionaryFromEPOSCcCouponCardInfo:couponCardModel] ;

    NSArray * couponarray = [[NSArray alloc] initWithObjects:ccouponcarddic, nil];
    
    NSMutableDictionary * dicpost = [[NSMutableDictionary alloc] initWithObjectsAndKeys:posHeadEntity,@"HeadEntity",posDetailEntitys,@"DetailEntitys",PayEntitys,@"PayEntitys",couponarray,@"CouponEntitys" ,nil];
    
    
    
    //修改数据
    if(couponCardModel == nil)
    {
        NSArray *emptyArr = [[NSArray alloc]init];
        [dicpost setValue:emptyArr forKey:@"CouponEntitys"];
    }
    
    NSMutableDictionary *bankEntityDic;
    NSArray *bankArr;
    NSMutableArray *saleBankInfos = [[NSMutableArray alloc]init];

    if (bankEntitySave==nil)
    {
        bankEntityDic = [EPOSBankEntity getDictionaryFromEPOSBankEntity:bankEntitySave];
        bankArr = [NSArray arrayWithObject:bankEntityDic];
        [posHeadEntity setObject:bankArr forKey:@"SaleBankInfos"];
    }
    else
    {
        [posHeadEntity setObject:saleBankInfos forKey:@"SaleBankInfos"];
    }
    
    [receiveJson setObject:posHeadEntity forKey:@"HeadEntity"];
    
    NSMutableDictionary *postDic = [NSMutableDictionary dictionaryWithObject:dicpost forKey:@"BillSaleWrap"];
   
    
    MBBaseServer *baseServer = [[MBBaseServer alloc]init];
    [baseServer setRequestStyle:nil andSetPathString:@"billsale_handler.ashx" andSetHeadString:nil andActionName:@"Save" andSetMainDictionary:postDic block:^(id json)
     {
         receiveSuccessSave(json);
     }
     failblock:^(NSError *error)
     {
         processFailure(error);
     }];
}







@end
